package reviewAlgorithms.dynamicProgramming;

public class Rob1 {
    public static void main(String[] args) {
        int[] nums = {1,2,1,1};
        System.out.println(rob(nums));
    }
    public static int rob(int[] nums) {
        if (nums == null || nums.length < 1) {
            return 0;
        }
        if (nums.length == 1) {
            return nums[0];
        }
        if (nums.length == 2) {
            return Math.max(nums[0],nums[1]);
        }
        if (nums.length == 3) {
            return Math.max(nums[2],Math.max(nums[0],nums[1]));
        }
        int k = nums.length - 1;
        int[] dp = new int[nums.length];
        dp[2] = nums[2];
        dp[3] = Math.max(nums[2],nums[3]);
        for (int i = 4; i < nums.length - 1; i++) {
            dp[i] = Math.max(dp[i - 1],dp[i - 2] + nums[i]);
        }
        int res1 = nums[0] + dp[k - 1];
        dp = new int[nums.length];
        dp[1] = nums[1];
        dp[2] = Math.max(nums[1],nums[2]);
        for (int i = 3; i < nums.length; i++) {
            dp[i] = Math.max(dp[i - 1],dp[i - 2] + nums[i]);
        }
        int res2 = dp[k];
        return Math.max(res1,res2);
    }
}
